//
//  BRMTimePickView.m
//  BrezzeSaaS
//
//  Created by macos on 17.3.22.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRMTimePickView.h"

@interface BRMTimePickView ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    UIView *_contentView;
    CGFloat _contentH;
    UIPickerView *_startPicker;
    UIPickerView *_endPicker;
    NSString *_lHour, *_lMin, *_rHour, *_rMin;
}

@property (nonatomic,strong) NSArray *leftTimes;
@property (nonatomic,strong) NSArray *rightTimes;

@end

@implementation BRMTimePickView

- (instancetype)init
{
    self = [super init];
    if (self) {
        _lHour = @"0";
        _lMin = @"00";
        _rHour = @"0";
        _rMin = @"00";
        _contentH = 264;
        [self setUI];
    }
    return self;
}

#pragma mark - func --
- (void)btnClick:(QMUIButton *)btn {
    NSInteger index = btn.tag - 200;
    [self br_hidden];
    if (index == 1) {
        // done
        if (self.timeDone) {
            NSString *times1 = [NSString stringWithFormat:@"%@:%@",_lHour,_lMin];
            NSString *times2 = [NSString stringWithFormat:@"%@:%@",_rHour,_rMin];
            self.timeDone(@[times1,times2]);
        }
    }
}

- (void)br_show {
    self.hidden = NO;
    [UIView animateWithDuration:0.35 animations:^{
        self->_contentView.transform = CGAffineTransformMakeTranslation(0, -self->_contentH);
    }];
}

- (void)br_hidden {
    
    [UIView animateWithDuration:0.35 animations:^{
        self->_contentView.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - pick delegate --
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (pickerView == _endPicker) {
       
        return [self.rightTimes count];
    }
    return self.leftTimes.count;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == _endPicker) {
       
        return [self.rightTimes[component] count];
    }
    return [self.leftTimes[component] count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (pickerView == _endPicker) {
        NSString *str = self.rightTimes[component][row];
        return str;
    }
    NSString *str = self.leftTimes[component][row];
    return str;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (pickerView == _endPicker) {
        
        NSString *curStr = self.rightTimes[component][row];
        NSLog(@"%@",curStr);
        if (component == 0) _rHour = curStr;
        if (component == 1) _rMin = curStr;
        return;
    }
    
    NSString *curStr = self.leftTimes[component][row];
    NSLog(@"%@",curStr);
    if (component == 0) _lHour = curStr;
    if (component == 1) _lMin = curStr;
}


#pragma mark - UI --
- (void)setUI {
    self.hidden = YES;
    self.backgroundColor = UIColorShadow;
    CGRect fram = CGRectMake(0, kScreenHeight, kScreenWidth, _contentH);
    UIView *contentView = [[UIView alloc] initWithFrame:fram];
    contentView.backgroundColor = UIColorWhite;
    [self addSubview:contentView];
    _contentView = contentView;
    NSArray *btns = @[Localized(@"public_cancel"),Localized(@"public_done")];
    for (int i = 0; i < btns.count; i++) {
        QMUIButton *btn = [[QMUIButton alloc] init];
        btn.tag = 200 + i;
        [btn setTitle:btns[i] forState:UIControlStateNormal];
        [btn setTitleColor:i == 0 ? UIColorPlaceholder :kColorMain forState:UIControlStateNormal];
        [contentView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.offset(16);
            }else {
                make.right.offset(-16);
            }
            make.top.offset(0);
            make.height.mas_equalTo(44);
            make.width.mas_greaterThanOrEqualTo(44);
        }];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    CGFloat pick_w = (kScreenWidth - 5) / 2.0;
    UIPickerView  *leftPickView=[[UIPickerView alloc]init];
    //设置普通代理对象为当前的视图控器

    leftPickView.delegate=self;

    //设置数据代理对象为当前视图控制器

    leftPickView.dataSource=self;

    [contentView addSubview:leftPickView];
    [leftPickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.offset(0);
        make.top.offset(44);
        make.width.mas_equalTo(pick_w);
    }];
    
    UIPickerView  *rightPickView=[[UIPickerView alloc]init];
    rightPickView.delegate=self;
    rightPickView.dataSource=self;

    [contentView addSubview:rightPickView];
    [rightPickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.offset(0);
        make.top.offset(44);
        make.width.mas_equalTo(pick_w);
    }];
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = UIColorText1;
    line.layer.cornerRadius = 1;
    line.layer.masksToBounds = YES;
    [contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(2);
//        make.width.mas_equalTo(5);
        make.left.equalTo(leftPickView.mas_right);
        make.right.equalTo(rightPickView.mas_left);
        make.centerY.equalTo(leftPickView);
    }];
    
    _startPicker = leftPickView;
    _endPicker = rightPickView;
    

    QMUILabel *mL = [BRToolsView br_label:@":" textColor:kColorTitle font:[UIFont fontM:17]];
    [contentView addSubview:mL];
    [mL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftPickView);
        make.centerX.equalTo(leftPickView);
    }];
    QMUILabel *mL2 = [BRToolsView br_label:@":" textColor:kColorTitle font:[UIFont fontM:17]];
    [contentView addSubview:mL2];
    [mL2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(rightPickView);
        make.centerX.equalTo(rightPickView);
    }];
}

- (NSArray *)leftTimes {
    
    if (_leftTimes.count == 0) {
        _leftTimes = [BRTimeTools br_timeDateHoursMin];
    }
    
    return _leftTimes;
}

- (NSArray *)rightTimes {
    if (_rightTimes.count == 0) {
        _rightTimes = [BRTimeTools br_timeDateHoursMin];
    }
    
    return _rightTimes;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
